package com.example.infowall02.service.impl;
import com.example.infowall02.domain.Articles;
import com.example.infowall02.domain.LikeRequest;
import com.example.infowall02.mapper.ArticlesMapper;
import com.example.infowall02.mapper.LikeMapper;
import com.example.infowall02.service.LikeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private ArticlesMapper articlesMapper;

    @Override
    public LikeRequest likeArticle(int userId, int articleId) {
        int num  = likeMapper.addLike(userId, articleId);
        articlesMapper.incrementLikes(articleId);
        LikeRequest like = likeMapper.getLike(userId,articleId);
        if (num > 0&&like!=null) {
            return like;
        }
        return null;
    }
    @Override
    public int unlikeArticle(int userId, int articleId) {
        int num =likeMapper.deleteLike(userId, articleId);
        articlesMapper.decrementLikes(articleId);
        LikeRequest like = likeMapper.getLike(userId,articleId);
        if (num > 0&&like!=null) {
            return num;
        }
        return 0;
    }
    @Override
    public int getUserLikeCount(int userId) {
        return likeMapper.countUserLikes(userId);
    }
    @Override
    public boolean isUserLikedArticle(int userId, int articleId) {
        return likeMapper.countLikes(userId, articleId);
    }
    @Override
    public List<Articles> getUserLikedArticles(int userId) {
        return likeMapper.getUserLikedArticles(userId);
    }
}
